#!/bin/sh

source scripts/common.sh

####################################################################################################

NETCDF_VERSION=4.0.1
NETCDF_BASENAME="netcdf"
NETCDF_ARCHIVE="${NETCDF_BASENAME}-${NETCDF_VERSION}.tar.gz"
NETCDF_DIR="${NETCDF_BASENAME}-${NETCDF_VERSION}"
NETCDF_URL="http://www.unidata.ucar.edu/downloads/netcdf/ftp/${NETCDF_ARCHIVE}"
NETCDF_PREFIX="${THIRD_PARTY_PREFIX}/${NETCDF_BASENAME}"
NETCDF_CPP_LIB=libnetcdf_c++.a
NETCDF_LIB=libnetcdf.a

####################################################################################################

function build_netcdf()
{

target=$1

mkdir -p ${THIRD_PARTY_BUILD} || exit 1
pushd ${THIRD_PARTY_BUILD} || exit 1

report ""
report "Removing old build from '${NETCDF_DIR}'..."
rm -fr $NETCDF_DIR
rm -fr $NETCDF_PREFIX/${target}

report "Preparing ${target} '${NETCDF_BASENAME}'..."
fetch $NETCDF_URL $NETCDF_ARCHIVE

report "Configuring ${target} '${NETCDF_BASENAME}'..."
report ""

pushd ${NETCDF_DIR} || exit 1
mkdir -p ${target} || exit 1

./configure \
		CXXFLAGS="${SYSROOT} -arch ${target}" \
		CFLAGS="${SYSROOT} -arch ${target}" \
		LDFLAGS="-Wl,-syslibroot,${DEVSDK} -arch ${target}" \
		--prefix=${NETCDF_PREFIX}/${target} \
        --target=${target}-apple-darwin \
        --enable-static \
        --disable-shared \
        --disable-f77 \
        --disable-f90 \
        --disable-examples 

if test $? -ne 0; then
        echo "configure failed"
        exit 1
fi

report "Building ${target} '${NETCDF_BASENAME}'..."
nice make || exit 1
nice make install || exit 1

report "Built ${target} '${NETCDF_BASENAME}'"
report ""

popd
popd

}

####################################################################################################

build_for_snow_leopard
for target in 'i686' 'x86_64' 'ppc'
do
	build_netcdf $target
done

create_universal_lib ${NETCDF_PREFIX} ${NETCDF_LIB}
create_universal_lib ${NETCDF_PREFIX} ${NETCDF_CPP_LIB}
create_intel_lib ${NETCDF_PREFIX} ${NETCDF_LIB}
create_intel_lib ${NETCDF_PREFIX} ${NETCDF_CPP_LIB}
create_powerpc_lib ${NETCDF_PREFIX} ${NETCDF_LIB}
create_powerpc_lib ${NETCDF_PREFIX} ${NETCDF_CPP_LIB}

####################################################################################################

